package self.hoy.demo.rabbitmq.workfair.work;

import com.rabbitmq.client.*;
import self.hoy.demo.rabbitmq.Constant;
import self.hoy.demo.rabbitmq.util.ConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author hyh
 * @date 2020/6/7
 */
public class Receive1 implements Constant {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        final Channel channel = connection.createChannel();
        //声明队列
        channel.queueDeclare(WORK_QUEUE_NAME, false, false, false, null);
        channel.basicQos(1);//每次不超过一条
        //定义一个消费者
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("【1】receive message : " + new String(body));
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("[1] done");
                    channel.basicAck(envelope.getDeliveryTag(), false);//手动回执
                }
            }
        };
        boolean isAutoAck = false;//手动回执
        channel.basicConsume(WORK_QUEUE_NAME, isAutoAck, consumer);
    }
}
